<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\observer\UserRefreshTokenObserver;

class UserRefreshToken extends Model
{
    use SoftDelete;

    protected $name = 'user_refresh_token';

    protected $autoWriteTimestamp = 'datetime';

    protected $observerClass = UserRefreshTokenObserver::class;

    protected $json = ['meta'];

    private string $operationType = 'other';

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function setOperationType(string $type): self
    {
        $this->operationType = $type;
        return $this;
    }

    public function getOperationType(): string
    {
        return $this->operationType;
    }

    public function getMetaAttr($value)
    {
        if (is_array($value)) {
            return $value;
        }
        return $value ? json_decode((string)$value, true) ?: [] : [];
    }

    public function setMetaAttr($value)
    {
        return $value;
    }
}
